package com.ai.dmc.index.web.agent.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class HttpUtils
{

  protected static HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

  public static String sendGet(String url, String param)
  {
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    String urlNameString = url + "?" + param;
    HttpGet httpGet = new HttpGet(urlNameString);
    String result = "";
    try
    {
      HttpResponse httpResponse = closeableHttpClient.execute(httpGet);

      HttpEntity entity = httpResponse.getEntity();
      if (entity != null) {
        result = StreamUtils.InputStreamTOString(entity.getContent(), "UTF-8");
//        logger.debug("请求成功：" + urlNameString);
      } else {
//        logger.debug("请求失败：" + urlNameString);
      }
    } catch (IOException e) {
//      logger.debug("读取" + urlNameString + "失败：" + e.getMessage());
      e.printStackTrace();
    }
    finally {
      try {
        closeableHttpClient.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  public static String sendPost(String url, String param)
  {
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    String urlNameString = url + "?" + param;
    HttpPost httpPost = new HttpPost(urlNameString);
    String result = "";
    try
    {
      HttpResponse httpResponse = closeableHttpClient.execute(httpPost);

      HttpEntity entity = httpResponse.getEntity();
      if (entity != null) {
        result = StreamUtils.InputStreamTOString(entity.getContent(), "UTF-8");
//        logger.debug("请求成功：" + urlNameString);
      } else {
//        logger.debug("请求失败：" + urlNameString);
      }
    } catch (IOException e) {
//      logger.debug("读取" + urlNameString + "失败：" + e.getMessage());
      e.printStackTrace();
    }
    finally {
      try {
        closeableHttpClient.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  public static void getImage(String ur, HttpServletRequest request, HttpServletResponse response)
  {
    try
    {
      String urlT = ur + "?" + request.getQueryString();
//      logger.debug("开始请求图片：" + urlT);
      URL url = new URL(urlT);
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      conn.setRequestMethod("GET");
      conn.setConnectTimeout(5000);
      InputStream inStream = conn.getInputStream();
      byte[] data = readInputStream(inStream);
      inStream.read(data);
      inStream.close();
      response.setContentType("image/*");
      OutputStream os = response.getOutputStream();
      os.write(data);
      os.flush();
      os.close();
    } catch (Exception e) {
//      logger.debug("图片请求失败：" + e.getMessage());
      e.printStackTrace();
    }
  }

  public static byte[] readInputStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, len);
    }
    inStream.close();
    return outStream.toByteArray();
  }
  
  public static void main(String[] args) {
	  
	  String url = "http://api.map.baidu.com/telematics/v3/weather?location=合肥&output=json&ak=BotOjSIxOHZGQwVYqKX3LX7t&callback=xxx";
	  String content = HttpUtils.sendGet(url, "");
	  
	  System.out.println(content);
	  
	  
  }
}